package com.woniuxy.sale.controller;

import com.woniuxy.commons.annotations.RequirePermission;
import com.woniuxy.commons.entity.sale.ChooseProduceMsg;
import com.woniuxy.commons.entity.sale.PackingList;
import com.woniuxy.commons.entity.sale.dto.ChooseProduceMsgDto;
import com.woniuxy.commons.entity.sale.vo.sale.PackingListVo;
import com.woniuxy.commons.result.Result;
import com.woniuxy.commons.service.sale.PurchaseManagerService;
import com.woniuxy.sale.service.ChooseProduceMsgService;
import com.woniuxy.sale.service.PackingListService;
import com.woniuxy.sale.testRabbitMQ.PackingListProducer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: CHL  <br/>
 * @ToDo: <br>
 * Date: 2022/4/19:14:31  <br/>
 * Description:
 */
@RestController
@RequestMapping("/sale/packingList")
@Api(tags = "打包单列表controller")
public class PackingListController {

    @Resource
    PackingListService packingListService;
    @Resource
    ChooseProduceMsgService chooseProduceMsgService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private PurchaseManagerService purchaseManagerService;

    @Resource
    private PackingListProducer packingListProducer;

    @RequirePermission("addPackingList")
    @ApiOperation(value = "测试微服务调用")
    @GetMapping("/testFeign")
    public Result testFeign(){
        return purchaseManagerService.selectAll();
    }

    @RequirePermission("addPackingList")
    @ApiOperation(value = "测试RabbitMQ")
    @GetMapping("/testRabbitMQ1")
    public Result testRabbitMQ1(){
//        System.out.println("测试1的url:"+request.getRequestURL());
            packingListProducer.send("测试rabbitMQ1","packingList");
        return Result.ok();
    }

    @RequirePermission("addPackingList")
    @ApiOperation(value = "测试RabbitMQ")
    @GetMapping("/testRabbitMQ2")
    public Result testRabbitMQ2(){
//        System.out.println("测试2的url:"+request.getRequestURL());
        packingListProducer.send("测试rabbitMQ2","packingMsg");
        return Result.ok();
    }

    @RequirePermission("addPackingList")
    @ApiOperation(value = "新增打包单列表打包单")
    @PostMapping("/insert")
    public Result insert(@RequestBody PackingList packingList) {
        System.out.println("新增大打包单");
        if ((packingListService.insert(packingList)) > 0) {
            return Result.ok("插入成功");
        }
        return Result.fail("插入失败");
    }

    @RequirePermission("addPackingList")
    @GetMapping("/findPM/{id}")
    @ApiOperation(value = "根据选货单id去选货的产品信息查询选货单选择的产品")
    public Result findPM(@PathVariable int id) {
//        BeanUtils.copyProperties();
        return chooseProduceMsgService.findChooseProduceBySoId(id);
    }

    //查询所有的打包单
    @RequirePermission("addPackingList")
    @PostMapping("/selectAll/{pageNum}/{pageSize}")
    @ApiOperation(value = "查询所有的打包单")
    public Result selectAll(@PathVariable("pageNum") Integer pageNum,
                            @PathVariable("pageSize") Integer pageSize,
                            @RequestBody PackingListVo packingListVo) {
        Result packingLists = packingListService.selectAll(pageNum, pageSize, packingListVo);

        String key = "selectAll::" + pageNum
                + ':' + pageSize + ':'
                + packingListVo.getPackingNumber();
//        if (packingListService.isExpire(key)==-1) {
//            redisTemplate.expire(key, 1, TimeUnit.MINUTES);
//        }
        return packingLists;
    }

    @RequirePermission("addPackingList")
    @PostMapping("/selectBy")
    @ApiOperation(value = "查询所有的打包单")
    public Result selectBy(@RequestBody PackingList packingList) {
        List<PackingList> packingLists = packingListService.selectBy(packingList);
        if (packingLists != null) {
            return Result.ok(packingLists);
        }
        return Result.fail();
    }
}
